package demo.database.show;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import com.titan.core.utils.db.DBUtils;
import com.titan.core.utils.db.RecordSet;

/**
 *
 *
 * @author 周成功
 * 
 */
public class ShowTableRows {
    public static void main(String[] args) {
        String[][] config = new String[][] {
//                {"oracle", "127.0.0.1", "1521", "ORCL", "etms3_base", "etms3_base"},
//                {"oracle", "127.0.0.1", "1521", "ORCL", "etms3", "etms3"}
//                {"oracle", "10.54.121.250", "1521", "CNNPS", "nbas", "nbas"}
//                {"oracle", "10.54.121.252", "1521", "GSKFDB", "mlos", "mlos"}
                {"oracle", "192.168.128.212", "1521", "ORCL", "hrbsgts", "hrbsgts"}
//            {"mysql", "127.0.0.1", "3306", "play", "root", "123456"}
        };
        
        Connection conn = null;
        try {
            for (int i = 0; i < config.length; i++) {
                String sql = null;
                if("oracle".equalsIgnoreCase(config[i][0])) {
                    sql = "select t.tname from tab t where t.tabtype='TABLE' and tname not in (SELECT object_name FROM user_recyclebin)";
                    conn = DBUtils.getOracleConnection(config[i][1], config[i][2], config[i][3], config[i][4], config[i][5], true);
                } else if("mysql".equalsIgnoreCase(config[i][0])) {
                    sql = "select table_name tname, table_comment from information_schema.tables where upper(table_schema) = upper('"+config[i][3]+"') and upper(table_type)='BASE TABLE'";
                    conn = DBUtils.getMySQLConnection(config[i][1], config[i][2], config[i][3], config[i][4], config[i][5], true);
                } else {
                    throw new RuntimeException("不知道的数据库类型["+config[i][0]+"]！");
                }
                
                RecordSet record = DBUtils.executeQueryToRecordSet(conn, sql);
                List<String> result = new ArrayList<String>();
                while(record.moveNext()) {
                    String tablename = record.getString("tname");
                    RecordSet rest = DBUtils.executeQueryToRecordSet(conn, "select count(*) from " + tablename);
                    if(rest.moveNext()) {
                        String count = rest.getString(1);
                        result.add(tablename+"\t\t"+count);
                    }
                }
                
                for (int j = 0; j < result.size(); j++) {
                    System.out.println(result.get(j));
                }
                
                DBUtils.close(conn);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(conn);
        }
    }

}

